﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;

namespace MediaBrowser.CustomControls.ScrollControl
{
    public delegate void ClickItemEvent(object sender);
    public delegate void MouseEnterItemEvent(object sender);
    public delegate void HideAnimationEndEvent(object sender);

    /// <summary>
    /// Logique d'interaction pour ItemHolder.xaml
    /// </summary>
    public partial class ItemHolder : UserControl
    {
        #region Data
        private AdornerLayer adornerLayer;
        private ItemHolderAdorner adorner;
        private FileInfo file;
        private Storyboard hideAnim;
        private Storyboard ShowAnim;

        private double initWidth;
        private double initHeight;

        #endregion

        public ItemHolder()
        {
            InitializeComponent();
            this.MouseEnter += new MouseEventHandler(ItemHolder_MouseEnter);
            this.MouseLeave += new MouseEventHandler(ItemHolder_MouseLeave);
            this.PreviewMouseDown += new MouseButtonEventHandler(ItemHolder_PreviewMouseDown);
            this.SetValue(HorizontalAlignmentProperty, HorizontalAlignment.Center);
            this.SetValue(VerticalAlignmentProperty, VerticalAlignment.Center);
            hideAnim = this.Resources["HideAnim"] as Storyboard;
            hideAnim.Completed += hideAnim_Completed;
            ShowAnim = this.Resources["ShowAnim"] as Storyboard;
            IsHide = false;

            initWidth = Width;
            initHeight = Height;
        }

        #region Public Methods/Properties

        /// <summary>
        /// The current file that thi class represents
        /// </summary>
        public FileInfo File
        {
            get { return file; }
            set
            {
                try
                {
                    file = value;
                    //set if its an image
                    if (file != null)
                    {
                        if (file.Name.IsImageFile())
                        {
                            BitmapImage img = new BitmapImage();
                            img.BeginInit();
                            img.CacheOption = BitmapCacheOption.OnLoad;
                            img.UriSource = new Uri(file.FullName);
                            img.EndInit();
                            imgSource.Source = img;
                        }
                    }
                }
                catch
                {
                    System.Diagnostics.Debug.WriteLine(string.Format(
                        "Problem with file {0} within ItemHolder property Setter",
                        file.Name));
                }
            }
        }

        public event ClickItemEvent OnClickItem;
        public event MouseEnterItemEvent OnMouseEnterItem;
        public event HideAnimationEndEvent OnHideAnimationEnd;

        public void Hide()
        {
            IsHide = true;
            hideAnim.Begin(this);
        }

        public void Show()
        {
            IsHide = false;
            ShowAnim.Begin(this);
        }

        public bool IsHide { get; set; }

        void hideAnim_Completed(object sender, EventArgs e)
        {
            if (OnHideAnimationEnd != null)
            {
                OnHideAnimationEnd(this);
            }
        }

        public List<Timeline> GetZoomOutAnimation(double delta)
        {
            ScaleTransform s = ((TransformGroup)this.RenderTransform).Children[0] as ScaleTransform;
            List<Timeline> lTmp = new List<Timeline>();
            if (Width >= 50)
            {
                double valRadius = (5 * Width) / 100;
                border.CornerRadius = new CornerRadius(valRadius);
                double valMargin = (4 * Width) / 100;
                imgSource.Margin = new Thickness(valMargin);

                if ((Width > 100) && (Width < 150))
                {
                    delta = delta * 2;
                }
                else if (Width >= 150)
                {
                    delta = delta * 4;
                }

                DoubleAnimation wAnim = new DoubleAnimation(Width, Width - delta, Duration.Automatic);
                Storyboard.SetTargetProperty(wAnim, new PropertyPath(ItemHolder.WidthProperty));
                Storyboard.SetTarget(wAnim, this);

                DoubleAnimation hAnim = new DoubleAnimation(Height, Height - delta, Duration.Automatic);
                Storyboard.SetTargetProperty(hAnim, new PropertyPath(ItemHolder.HeightProperty));
                Storyboard.SetTarget(hAnim, this);

                lTmp.Add(wAnim);
                lTmp.Add(hAnim);
            }
            return lTmp;
        }

        public List<Timeline> GetZoomInAnimation(double delta)
        {
            List<Timeline> lTmp = new List<Timeline>();
            if (Width <= 200)
            {
                double valRadius = (5 * Width) / 100;
                border.CornerRadius = new CornerRadius(valRadius);
                double valMargin = (4 * Width) / 100;
                imgSource.Margin = new Thickness(valMargin);

                if ((Width > 100) && (Width < 150))
                {
                    delta = delta * 2;
                }
                else if (Width >= 150)
                {
                    delta = delta * 4;
                }

                DoubleAnimation wAnim = new DoubleAnimation(Width, Width + delta, Duration.Automatic);
                Storyboard.SetTargetProperty(wAnim, new PropertyPath(ItemHolder.WidthProperty));
                Storyboard.SetTarget(wAnim, this);

                DoubleAnimation hAnim = new DoubleAnimation(Height, Height + delta, Duration.Automatic);
                Storyboard.SetTargetProperty(hAnim, new PropertyPath(ItemHolder.HeightProperty));
                Storyboard.SetTarget(hAnim, this);

                lTmp.Add(wAnim);
                lTmp.Add(hAnim);
            }
            return lTmp;
        }

        public List<Timeline> GetZoomFitAnimation()
        {
            List<Timeline> lTmp = new List<Timeline>();
            border.CornerRadius = new CornerRadius(5);
            imgSource.Margin = new Thickness(4);

            DoubleAnimation wAnim = new DoubleAnimation(Width, initWidth, Duration.Automatic);
            Storyboard.SetTargetProperty(wAnim, new PropertyPath(ItemHolder.WidthProperty));
            Storyboard.SetTarget(wAnim, this);

            DoubleAnimation hAnim = new DoubleAnimation(Height, initHeight, Duration.Automatic);
            Storyboard.SetTargetProperty(hAnim, new PropertyPath(ItemHolder.HeightProperty));
            Storyboard.SetTarget(hAnim, this);

            lTmp.Add(wAnim);
            lTmp.Add(hAnim);
            return lTmp;
        }

        #endregion

        #region Private Methods

        void ItemHolder_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.RightButton == MouseButtonState.Pressed)
            {
                if (OnClickItem != null)
                {
                    OnClickItem(this);
                }
            }
        }

        private void ItemHolder_MouseLeave(object sender, MouseEventArgs e)
        {
            this.Opacity = 0.6;
            //remove adorner
            if (adorner != null)
            {
                adornerLayer.Remove(adorner);
                adorner = null;
            }
        }

        private void ItemHolder_MouseEnter(object sender, MouseEventArgs e)
        {
            this.Opacity = 1.0;

            //and add an adorner which shows this element as a VisualBrush
            adornerLayer = AdornerLayer.GetAdornerLayer(this);
            if (adorner == null)
            {
                adorner = new ItemHolderAdorner(this,
                    TranslatePoint(new Point(0, 0),
                    this.Parent as UIElement));
                adornerLayer.Add(adorner);
            }

            if (OnMouseEnterItem != null)
            {
                OnMouseEnterItem(this);
            }
        }

        #endregion

    }
}
